package compare;

import java.util.*;

class Person implements Comparable<Person> {  
    String name;  
    int age;  

    Person(String name, int age) {  
        this.name = name;  
        this.age = age;  
    }  

    @Override  
    public int compareTo(Person other) {  
        return other.age - this.age; // 按年龄降序排序
    }  

    @Override  
    public String toString() {  
        return name + " (" + age + ")";  
    }  
}
class PersonComparator{


    public static void main(String[] args) {
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("Alice", 30));
        people.add(new Person("Bob", 25));
        people.add(new Person("Charlie", 35));
        Collections.sort(people, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.name.compareTo(o1.name);
            }
        });

        System.out.println(people);
    }







}
class PriorityQueueDemo {


    public static void main(String[] args) {


        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1,Integer o2) {
                return o2-o1;
            }
        });

//        queue.addAll(Arrays.asList(3,2,4,1,5));
        queue.add(3);queue.add(1);queue.add(2);queue.add(4);queue.add(5);
        while(!queue.isEmpty()) {
            System.out.print((queue.poll()));
        }

     }


}

public class ComparableDemo {  
    public static void main(String[] args) {  
        ArrayList<Person> people = new ArrayList<>();
        people.add(new Person("Bob", 25));  
        people.add(new Person("Charlie", 35));  

        Collections.sort(people); // 使用 Comparable 进行排序

        System.out.println("排序后 (按年龄): " + people);  
    }  
}

